package cn.stylefeng.guns.modular.jobTask.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.guns.modular.jobTask.entity.JobTask;
import cn.stylefeng.guns.modular.jobTask.enums.JobTaskExceptionEnum;
import cn.stylefeng.guns.modular.jobTask.mapper.JobTaskMapper;
import cn.stylefeng.guns.modular.jobTask.pojo.request.JobTaskRequest;
import cn.stylefeng.guns.modular.jobTask.pojo.result.JobTaskNodeResult;
import cn.stylefeng.guns.modular.jobTask.service.JobTaskService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.constants.TreeConstants;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.roses.kernel.rule.tree.factory.DefaultTreeBuildFactory;
import cn.stylefeng.roses.kernel.sys.modular.org.service.HrOrganizationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 任务表业务实现层
 *
 * @author LiZh
 * @date 2024.07.08
 */
@Service
public class JobTaskServiceImpl extends ServiceImpl<JobTaskMapper, JobTask> implements JobTaskService {


    @Override
    public void add(JobTaskRequest jobTaskRequest) {
        JobTask jobTask = new JobTask();
        BeanUtil.copyProperties(jobTaskRequest, jobTask);
        fillParentIds(jobTask);
        this.save(jobTask);
    }

    public static void fillParentIds(JobTask jobTask) {
        if (TreeConstants.DEFAULT_PARENT_ID.equals(jobTask.getParentId())) {
            jobTask.setPids("[" + TreeConstants.DEFAULT_PARENT_ID + "]" + ",");
        } else {
            JobTaskService jobTaskService = (JobTaskService) SpringUtil.getBean(JobTaskService.class);
            JobTaskRequest jobTaskRequest = new JobTaskRequest();
            jobTaskRequest.setTaskId(jobTask.getParentId());
            JobTask jobTempTask = jobTaskService.detail(jobTaskRequest);
            jobTask.setPids(jobTempTask.getPids() + "[" + jobTempTask.getTaskId() + "]" + ",");
        }

    }

    @Override
    public void del(JobTaskRequest jobTaskRequest) {
        JobTask jobTask = this.queryJobTask(jobTaskRequest);
        jobTask.setDelFlag(YesOrNotEnum.Y.getCode());
        //this.removeById(jobTask.getTaskId());
        this.updateById(jobTask);
    }

    @Override
    public void edit(JobTaskRequest jobTaskRequest) {
        JobTask jobTask = this.queryJobTask(jobTaskRequest);
        BeanUtil.copyProperties(jobTaskRequest, jobTask);
        fillParentIds(jobTask);
        jobTask.setDelFlag(YesOrNotEnum.N.getCode());
        this.updateById(jobTask);
    }

    @Override
    public JobTask detail(JobTaskRequest jobTaskRequest) {
        return this.queryJobTask(jobTaskRequest);
    }

    @Override
    public PageResult<JobTask> findPage(JobTaskRequest jobTaskRequest) {
        LambdaQueryWrapper<JobTask> wrapper = createWrapper(jobTaskRequest);
        Page<JobTask> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<JobTaskNodeResult> tree(JobTaskRequest jobTaskRequest) {
        List<JobTask> jobTaskList = findList(jobTaskRequest);
        List<JobTaskNodeResult> jobTaskNodeResultList = new ArrayList<>();
        for (JobTask jobTask : jobTaskList) {
            JobTaskNodeResult temp = new JobTaskNodeResult();
            BeanUtil.copyProperties(jobTask, temp);
            jobTaskNodeResultList.add(temp);
        }
        return (new DefaultTreeBuildFactory()).doTreeBuild(jobTaskNodeResultList);
    }

    @Override
    public List<JobTask> findList(JobTaskRequest jobTaskRequest) {
        LambdaQueryWrapper<JobTask> wrapper = this.createWrapper(jobTaskRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author LiZh
     * @date 2024.07.08
     */
    private JobTask queryJobTask(JobTaskRequest jobTaskRequest) {
        JobTask jobTask = this.getById(jobTaskRequest.getTaskId());
        if (ObjectUtil.isEmpty(jobTask)) {
            throw new ServiceException(JobTaskExceptionEnum.JOB_TASK_NOT_EXISTED);
        }
        return jobTask;
    }

    /**
     * 创建查询wrapper
     *
     * @author LiZh
     * @date 2024.07.08
     */
    private LambdaQueryWrapper<JobTask> createWrapper(JobTaskRequest jobTaskRequest) {
        LambdaQueryWrapper<JobTask> queryWrapper = new LambdaQueryWrapper<>();

        Long taskId = jobTaskRequest.getTaskId();
        String taskName = jobTaskRequest.getTaskName();
        Long parentId = jobTaskRequest.getParentId();
        String pids = jobTaskRequest.getPids();
        String message = jobTaskRequest.getMessage();
        String taskLevel = jobTaskRequest.getTaskLevel();
        String complete = jobTaskRequest.getComplete();
        Date deadLine = jobTaskRequest.getDeadLine();
        String delFlag = jobTaskRequest.getDelFlag();

        queryWrapper.eq(ObjectUtil.isNotNull(taskId), JobTask::getTaskId, taskId);
        queryWrapper.like(ObjectUtil.isNotEmpty(taskName), JobTask::getTaskName, taskName);
        queryWrapper.eq(ObjectUtil.isNotNull(parentId), JobTask::getParentId, parentId);
        queryWrapper.like(ObjectUtil.isNotEmpty(pids), JobTask::getPids, pids);
        queryWrapper.like(ObjectUtil.isNotEmpty(message), JobTask::getMessage, message);
        queryWrapper.like(ObjectUtil.isNotEmpty(taskLevel), JobTask::getTaskLevel, taskLevel);
        queryWrapper.like(ObjectUtil.isNotEmpty(complete), JobTask::getComplete, complete);
        queryWrapper.eq(ObjectUtil.isNotNull(deadLine), JobTask::getDeadLine, deadLine);
        queryWrapper.like(ObjectUtil.isNotEmpty(delFlag), JobTask::getDelFlag, delFlag);

        return queryWrapper;
    }

}